package com.zhentao.exception;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 业务异常
 * 
 * @author zhentao
 * @date 2024-12-19
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class BusinessException extends RuntimeException {

    private String code;
    private String message;

    public BusinessException(String message) {
        super(message);
        this.code = "BUSINESS_ERROR";
        this.message = message;
    }

    public BusinessException(String code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public BusinessException(String code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.message = message;
    }

    // 常用业务异常静态方法
    public static BusinessException of(String message) {
        return new BusinessException(message);
    }

    public static BusinessException of(String code, String message) {
        return new BusinessException(code, message);
    }

    // 参数相关异常
    public static BusinessException paramNotFound(String paramKey) {
        return new BusinessException("PARAM_NOT_FOUND", "参数不存在: " + paramKey);
    }

    public static BusinessException paramKeyExists(String paramKey) {
        return new BusinessException("PARAM_KEY_EXISTS", "参数键已存在: " + paramKey);
    }

    public static BusinessException paramValueInvalid(String paramKey, String reason) {
        return new BusinessException("PARAM_VALUE_INVALID", 
                String.format("参数值无效: %s, 原因: %s", paramKey, reason));
    }

    // 规则相关异常
    public static BusinessException ruleNotFound(String ruleCode) {
        return new BusinessException("RULE_NOT_FOUND", "规则不存在: " + ruleCode);
    }

    public static BusinessException ruleDisabled(String ruleCode) {
        return new BusinessException("RULE_DISABLED", "规则已禁用: " + ruleCode);
    }

    public static BusinessException ruleExecutionFailed(String ruleCode, String reason) {
        return new BusinessException("RULE_EXECUTION_FAILED", 
                String.format("规则执行失败: %s, 原因: %s", ruleCode, reason));
    }

    // 分类相关异常
    public static BusinessException categoryNotFound(String categoryCode) {
        return new BusinessException("CATEGORY_NOT_FOUND", "分类不存在: " + categoryCode);
    }

    public static BusinessException categoryHasChildren(String categoryCode) {
        return new BusinessException("CATEGORY_HAS_CHILDREN", "分类下存在子分类，无法删除: " + categoryCode);
    }

    // 推荐相关异常
    public static BusinessException recommendationNotFound(Long recommendationId) {
        return new BusinessException("RECOMMENDATION_NOT_FOUND", "推荐记录不存在: " + recommendationId);
    }

    public static BusinessException recommendationExpired(Long recommendationId) {
        return new BusinessException("RECOMMENDATION_EXPIRED", "推荐记录已过期: " + recommendationId);
    }

    // 缓存相关异常
    public static BusinessException cacheOperationFailed(String operation) {
        return new BusinessException("CACHE_OPERATION_FAILED", "缓存操作失败: " + operation);
    }
}
